using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VABVANZ
    {
        public DataRow Convert2ABVANZ(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryABVANZ(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["ABVMANDANT"] = " ".PadRight(2 ); 
         dwReturn["ABVAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["ABVAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["ABVAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["ABVAFOLGE"] = " ".PadRight(3 ); 
         dwReturn["ABVANLIEF"] = " ".PadRight(4 ); 
         dwReturn["ABVAENZL"] = " ".PadRight(0 ); 
         dwReturn["ABVAENB"] = " ".PadRight(8 ); 
         dwReturn["ABVAEND"] = " ".PadRight(0 ); 
         dwReturn["ABVNEUB"] = " ".PadRight(8 ); 
         dwReturn["ABVNEUD"] = " ".PadRight(0 ); 
         dwReturn["ABVLIEFNR"] = " ".PadRight(8 ); 
         dwReturn["ABVVANZ"] = " ".PadRight(15 ); 
         dwReturn["ABVVADAT"] = " ".PadRight(0 ); 
         dwReturn["ABVANZSTK"] = " ".PadRight(0 ); 
         dwReturn["ABVROHMTR"] = " ".PadRight(0 ); 
         dwReturn["ABVBRUTTO"] = " ".PadRight(0 ); 
         dwReturn["ABVNETTO"] = " ".PadRight(0 ); 
         dwReturn["ABVVERDERB"] = " ".PadRight(0 ); 
         dwReturn["ABVFOLGEMTR"] = " ".PadRight(0 ); 
         dwReturn["ABVUMARB"] = " ".PadRight(1 ); 
         dwReturn["ABVKOMP"] = " ".PadRight(1 ); 
         dwReturn["ABVOB"] = " ".PadRight(1 ); 
         dwReturn["ABVBERECH"] = " ".PadRight(1 ); 
         dwReturn["ABVHANDEL"] = " ".PadRight(0 ); 
         dwReturn["ABVFILLER"] = " ".PadRight(47 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapABVANZ(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveABVANZ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryABVANZ(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapABVANZ(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertABVANZ(dwLocal, ITXDBVERSION);
            }
            UpdateABVANZ(dwLocal);
        }
        public void InsertABVANZ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO ABVANZ(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryABVANZ(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM ABVANZ WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM ABVANZ  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ABVANZ";
            Return = Result;
            return Return;
        }
        public void DeleteABVANZ(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ABVANZ   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateABVANZ(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE ABVANZ SET "
                    +"ABVMANDANT= :ABVMANDANT,"
                    +"ABVAUFKRS= :ABVAUFKRS,"
                    +"ABVAUFLFD= :ABVAUFLFD,"
                    +"ABVAUFPOS= :ABVAUFPOS,"
                    +"ABVAFOLGE= :ABVAFOLGE,"
                    +"ABVANLIEF= :ABVANLIEF,"
                    +"ABVAENZL= :ABVAENZL,"
                    +"ABVAENB= :ABVAENB,"
                    +"ABVAEND= :ABVAEND,"
                    +"ABVNEUB= :ABVNEUB,"
                    +"ABVNEUD= :ABVNEUD,"
                    +"ABVLIEFNR= :ABVLIEFNR,"
                    +"ABVVANZ= :ABVVANZ,"
                    +"ABVVADAT= :ABVVADAT,"
                    +"ABVANZSTK= :ABVANZSTK,"
                    +"ABVROHMTR= :ABVROHMTR,"
                    +"ABVBRUTTO= :ABVBRUTTO,"
                    +"ABVNETTO= :ABVNETTO,"
                    +"ABVVERDERB= :ABVVERDERB,"
                    +"ABVFOLGEMTR= :ABVFOLGEMTR,"
                    +"ABVUMARB= :ABVUMARB,"
                    +"ABVKOMP= :ABVKOMP,"
                    +"ABVOB= :ABVOB,"
                    +"ABVBERECH= :ABVBERECH,"
                    +"ABVHANDEL= :ABVHANDEL,"
                    +"ABVFILLER= :ABVFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("ABVMANDANT", OracleType.NVarChar).Value = dwData["ABVMANDANT"].ToString(); 
          or1.Parameters.Add("ABVAUFKRS", OracleType.NVarChar).Value = dwData["ABVAUFKRS"].ToString(); 
          or1.Parameters.Add("ABVAUFLFD", OracleType.NVarChar).Value = dwData["ABVAUFLFD"].ToString(); 
          or1.Parameters.Add("ABVAUFPOS", OracleType.NVarChar).Value = dwData["ABVAUFPOS"].ToString(); 
          or1.Parameters.Add("ABVAFOLGE", OracleType.NVarChar).Value = dwData["ABVAFOLGE"].ToString(); 
          or1.Parameters.Add("ABVANLIEF", OracleType.NVarChar).Value = dwData["ABVANLIEF"].ToString(); 
          or1.Parameters.Add("ABVAENZL", OracleType.NVarChar).Value = dwData["ABVAENZL"].ToString(); 
          or1.Parameters.Add("ABVAENB", OracleType.NVarChar).Value = dwData["ABVAENB"].ToString(); 
          or1.Parameters.Add("ABVAEND", OracleType.NVarChar).Value = dwData["ABVAEND"].ToString(); 
          or1.Parameters.Add("ABVNEUB", OracleType.NVarChar).Value = dwData["ABVNEUB"].ToString(); 
          or1.Parameters.Add("ABVNEUD", OracleType.NVarChar).Value = dwData["ABVNEUD"].ToString(); 
          or1.Parameters.Add("ABVLIEFNR", OracleType.NVarChar).Value = dwData["ABVLIEFNR"].ToString(); 
          or1.Parameters.Add("ABVVANZ", OracleType.NVarChar).Value = dwData["ABVVANZ"].ToString(); 
          or1.Parameters.Add("ABVVADAT", OracleType.NVarChar).Value = dwData["ABVVADAT"].ToString(); 
          or1.Parameters.Add("ABVANZSTK", OracleType.NVarChar).Value = dwData["ABVANZSTK"].ToString(); 
          or1.Parameters.Add("ABVROHMTR", OracleType.NVarChar).Value = dwData["ABVROHMTR"].ToString(); 
          or1.Parameters.Add("ABVBRUTTO", OracleType.NVarChar).Value = dwData["ABVBRUTTO"].ToString(); 
          or1.Parameters.Add("ABVNETTO", OracleType.NVarChar).Value = dwData["ABVNETTO"].ToString(); 
          or1.Parameters.Add("ABVVERDERB", OracleType.NVarChar).Value = dwData["ABVVERDERB"].ToString(); 
          or1.Parameters.Add("ABVFOLGEMTR", OracleType.NVarChar).Value = dwData["ABVFOLGEMTR"].ToString(); 
          or1.Parameters.Add("ABVUMARB", OracleType.NVarChar).Value = dwData["ABVUMARB"].ToString(); 
          or1.Parameters.Add("ABVKOMP", OracleType.NVarChar).Value = dwData["ABVKOMP"].ToString(); 
          or1.Parameters.Add("ABVOB", OracleType.NVarChar).Value = dwData["ABVOB"].ToString(); 
          or1.Parameters.Add("ABVBERECH", OracleType.NVarChar).Value = dwData["ABVBERECH"].ToString(); 
          or1.Parameters.Add("ABVHANDEL", OracleType.NVarChar).Value = dwData["ABVHANDEL"].ToString(); 
          or1.Parameters.Add("ABVFILLER", OracleType.NVarChar).Value = dwData["ABVFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
